/*
 * 作者：lepon
 * QQ：895115190
 * 代码请保留作者信息
 */
var WebsocketHeartbeatJs = require("./index.js");
// url: 'ws://192.168.101.4:9317/device/get_device_list',//ip地址，请修改为websocket服务端对应地址

const options = {
  url: "ws://192.168.101.4:9317", //ip地址，请修改为websocket服务端对应地址
  pingTimeout: 5000,
  pongTimeout: 3000,
  reconnectTimeout: 3000,
  pingMsg: "heart beat",
  repeatLimit: 5,
};

let websocketHeartbeatJs = new WebsocketHeartbeatJs(options);

websocketHeartbeatJs.onopen = function () {
  log("connect success");
  websocketHeartbeatJs.send("hello aj server");
  log("send hello");
};

websocketHeartbeatJs.onmessage = function (msg, ws) {
  log("onmessage: " + msg);
  console.info("收到文本消息: ", text);
  let info = JSON.parse(text);
  log(info);
  let type = info.type;
  if (type === "command") {
    let scriptContent = info.data.script;
    log("scriptContent =");
    log(scriptContent);
    engines.execScript("hello world", scriptContent);
    return;
  }

  if (msg == "close") {
    websocketHeartbeatJs.close();
    return true;
  }
};

websocketHeartbeatJs.onreconnect = function () {
  log("reconnecting...");
};

websocketHeartbeatJs.onclose = function () {
  log("close...");
};

setInterval(() => {}, 1000);
